package com.hsf.springboot.elasticsearch.Configuartion;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    @Profile(value="dev")
    public DataSource dataSource(){
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/mall?serverTimezone==Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=UTF-8");
        hikariConfig.setUsername("druid");
        hikariConfig.setPassword("druid705");
        hikariConfig.setConnectionTimeout(3000);
        hikariConfig.setMinimumIdle(5);
        hikariConfig.setMaximumPoolSize(15);
        hikariConfig.setAutoCommit(true);
        hikariConfig.setMaxLifetime(3000);
        hikariConfig.setConnectionTestQuery("SELECT 1 from dual");
        HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);
        return hikariDataSource;
    }

    @Bean(name="txManager")
    @Profile(value="dev")
    public DataSourceTransactionManager transactionManager(){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();

        transactionManager.setDataSource(dataSource());

        return transactionManager;

    }
}
